import Mock from "mockjs"
const result = Mock.mock({
    data: [
        {
            id: '@increment',
            name: '@cname()',
            address: '@city(true)'
        },
        {
            id: '@increment',
            name: '@cname()',
            address: '@city(true)'
        },
        {
            id: '@increment',
            name: '@cname()',
            address: '@city(true)'
        },
        {
            id: '@increment',
            name: '@cname()',
            address: '@city(true)'
        },
        {
            id: '@increment',
            name: '@cname()',
            address: '@city(true)'
        }
    ]
})
// 模拟一个不需要传参的 get 请求
Mock.mock('list', 'get', () => {
    return {
        status: 200,
        message: '数据获取成功',
        data: result.data
    }
})
/*模拟删除数据的方式*/
Mock.mock(`delete`,'delete',function(options){
    const {id}= JSON.parse(options.body)
    const arr = result.data
    let index=-1
    for(var i in arr){
        if(arr[i].id==id){//在数组arr里找到这个id
            index=i;
        }
    } 
    if(index>=0){
        result.data.splice(index,1)
        const data= {
            data:result.data,
            status:200,
            message:"删除成功！"
        }
        return data;//返回这个数组,也就是返回处理后的假数据
    }  
})
/*模拟修改数据的方式*/
Mock.mock(`edit`,'put',function(options){
    const {data:res}= JSON.parse(options.body)
    for(var i in result.data){
        if(result.data[i].id==res.id){//在数组arr里找到这个id
            result.data[i].name = res.name
            result.data[i].address = res.address
            const data= {
                status:200,
                message:"修改成功！"
            }
            return data;
        }
    }   
})
